﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedGlove.Lib;

namespace RGPWEB
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btn_login.Attributes.Add("onclick", "return CheckLogin()");
        }

        protected void btn_login_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["VerifyCode"] == null)
            {
              //  JScript.Alert("您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统");
            }
            //验证码是否证确
            else 
          // if (String.Compare(Request.Cookies["VerifyCode"].Value, Request.Form["txt_verify"].Trim().ToUpper(), true) == 0)
            {
                RGP.BLL.Users bll = new RGP.BLL.Users();
                RGP.Model.Users model = new RGP.Model.Users();

                if (Request.Form["txt_name"].Trim() == "" || Request.Form["txt_password"].Trim() == "")
                {
                    JScript.Alert("请输入完整的登录信息!");
                }
                else
                {
                    if (bll.CheckLogin(Request.Form["txt_name"].Trim(), SecurityEncryption.MD5(Request.Form["txt_password"].Trim().ToLower(), 32)))
                    {
                        model = bll.GetUserModel(Request.Form["txt_name"].Trim());
                        if (model.RoleID.Count != 0 || model.IsLimit == true)
                        {
                            if (model.Status != 0)
                            {
                                bll.UpdateLoginTime(model.UserID);
                                SessionBox.CreateUserSession(new UserSession(model.UserID, model.UserName, model.RoleID, model.UserGroup, model.IsLimit, model.Status));
                                Response.Redirect("~/admin/main.aspx");
                            }
                            else
                            {
                                JScript.Alert("用户或密码错误!");
                            }
                        }
                        else
                        {
                            JScript.Alert("用户还未激活,请与管理员联系!");
                        }
                    }
                    else
                    {
                        JScript.Alert("用户或密码错误!");
                    }
                }
             }
          //  else
          //  {
          //      JScript.Alert("验证码错误!");
          //  }
        }
    }
}
